package com.zhangxujie.litewords.api;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import com.zhangxujie.litewords.R;
import com.zhangxujie.litewords.db.AppDatabase;
import com.zhangxujie.litewords.db.DBUtil;
import com.zhangxujie.litewords.entity.Achievement;

//成就
public class AchievementUtil {

    public static void checkReciteWordsNums(final Context ctx, final int count) {
        //TODO:添加成就检测

        new Thread() {

            @Override
            public void run() {
                final AppDatabase db = DBUtil.getDb(ctx);
//                int dbCount = db.reciteWordsDAO().count();
                if (count >= 10) {
                    Achievement achievement = db.achievementDAO().findById(1);
                    achievement.status = 1;
                    achievement.timestamp = String.valueOf(System.currentTimeMillis());
                    db.achievementDAO().update(achievement);
                    addNotification(ctx, achievement.achievementName);
                } else if (count >= 40){
                    Achievement achievement = db.achievementDAO().findById(2);
                    achievement.status = 1;
                    achievement.timestamp = String.valueOf(System.currentTimeMillis());
                    db.achievementDAO().update(achievement);
                    addNotification(ctx, achievement.achievementName);

                }

                super.run();
            }
        }.start();
//        ListView list = view.findViewById(R.id.achi_list);
//        int a = list.getCount();
//        System.out.println(a);
//        Log.d("AchievementUtil", "checkReciteWordsNums: " + a);
    }


    private static void addNotification(Context ctx, String achievementName){
        NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, "achievement")
                .setSmallIcon(R.mipmap.ic_logo)
                .setContentTitle("LiteWords")
                .setContentText("恭喜您获得新成就：" + achievementName)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(ctx);

        // notificationId is a unique int for each notification that you must define
        notificationManager.notify(1, builder.build());
    }


}
